package com.bootdo.wx.service.impl;

import cn.hutool.core.map.MapUtil;
import com.bootdo.wx.dao.*;
import com.bootdo.wx.domain.GoodsGoodsDO;
import com.bootdo.wx.domain.GoodsProductDO;
import com.bootdo.wx.domain.MemberUserDO;
import com.bootdo.wx.service.GoodsProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;


@Service
public class GoodsProductServiceImpl implements GoodsProductService {
	@Autowired
	private GoodsProductDao goodsProductDao;
	@Autowired
	private GoodsGoodsDao goodsGoodsDao;
	@Autowired
	private MemberUserDao memberUserDao;
/*	@Autowired
	private GoodsSpikeService goodsSpikeService;
	@Autowired
	private CmsContentDao cmsContentDao;
	@Autowired
	private OrderOrderService orderOrderService;*/
	@Autowired
	private MemberAddressDao memberAddressDao;
	@Autowired
	private ShopExpressMoneyDao shopExpressMoneyDao;
	
	
	@Override
	public GoodsProductDO get(String id){
		return goodsProductDao.get(id);
	}
	
	@Override
	public List<GoodsProductDO> list(Map<String, Object> map){
		return goodsProductDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return goodsProductDao.count(map);
	}
	
	@Override
	public int save(GoodsProductDO goodsProduct){
		return goodsProductDao.save(goodsProduct);
	}
	
	@Override
	public int update(GoodsProductDO goodsProduct){
		return goodsProductDao.update(goodsProduct);
	}
	
	@Override
	public int remove(String id){
		return goodsProductDao.remove(id);
	}
	
	@Override
	public int batchRemove(String[] ids){
		return goodsProductDao.batchRemove(ids);
	}
	
	/**
	 * 生成货号前缀的属性
	 */
	private static final char[] _UU32 = "0123456789abcdefghijklmnopqrstuv".toCharArray();
	
	/**
	 * 生成货号前缀的方法
	 * @param uu
	 * @return
	 */
	public static String UU32(UUID uu) {
		StringBuilder sb = new StringBuilder();
		long m = uu.getMostSignificantBits();
		long l = uu.getLeastSignificantBits();
		int i;
		for(i = 0; i < 13; ++i) {
			sb.append(_UU32[(int)(m >> (13 - i - 1) * 5) & 31]);
		}
		for(i = 0; i < 13; ++i) {
			sb.append(_UU32[(int)(l >> (13 - i - 1) * 5) & 31]);
		}
		return sb.toString();
	}

	@Override
	public String getSkuPrefix() {
		String uuid = "S" + UU32(UUID.randomUUID()).substring(1, 12).toUpperCase();
		List<GoodsProductDO> list = goodsProductDao.queryBySku(uuid);
		if (list!=null && list.size()>0){
			return getSkuPrefix();
		}
		return uuid;
	}

	/*真删除*/
	@Override
	public int removeByGoodsId(String goodsId) {
		return goodsProductDao.removeByGoodsId(goodsId);
	}

	/*假删除*/
	@Override
	public int removeByGoodsIdJ(String goodsId) {
		return goodsProductDao.removeByGoodsIdJ(goodsId);
	}

	@Override
	public Map<String,Object> getByGoodsIdAndSpec(Map<String, Object> params) {
		String type = "0";
		String userId = params.get("userId").toString();
		MemberUserDO userDO = memberUserDao.get(userId);
		Map<String,Object> productMap = new HashMap<String,Object>();
		if ("0".equals(type)) {
			productMap =goodsProductDao.getByGoodsIdAndSpec(params);
		} else if ("1".equals(type)) {
			productMap =goodsProductDao.getBySpikeIdAndSpec(params);
		} else if ("2".equals(type)) {
			productMap =goodsProductDao.getByNineIdAndSpec(params);
		} else if ("4".equals(type)) {
			productMap =goodsProductDao.getByTeamIdAndSpec(params);
		} else if ("5".equals(type)) {
			productMap =goodsProductDao.getByUpgradeIdAndSpec(params);
		}
		if(productMap == null){
			return productMap;
		}
		GoodsGoodsDO goodsGoodsDO =goodsGoodsDao.get(productMap.get("goods_id").toString());
		//int isvip = goodsGoodsDO.getIsvip();
		int level = 0;
		if(userDO!=null){
			level =userDO.getLevel();
		}
		//int postSize = goodsGoodsDO.getPostSize();  //运费模板类型  0：自定义运费，1：按重量模板
		//double weight = Double.parseDouble(productMap.get("weight").toString());
		//BigDecimal postFee = new BigDecimal(productMap.get("post_fee").toString());

			switch (level) {
				case 1 :
					productMap.put("price", productMap.get("price_c"));
					break;
				case 2 :
					productMap.put("price", productMap.get("price_z"));
					break;
				case 3 :
					productMap.put("price", productMap.get("price_g"));
					break;
				default:
					productMap.put("price",productMap.get("price"));

		}
		if ("5".equals(type)) {
			productMap.put("price",productMap.get("price"));
		}
		/*if (postSize == 0) {
			productMap.put("post_fee",postFee);
		} else {

			Map<String, Object> map = new HashMap<>();
			map.put("userId", userId);
			map.put("defaultValue", "0");
			List<MemberAddressDO> queryByUserId = memberAddressDao.queryByUserId(map);
			if(queryByUserId.size()>0){
				MemberAddressDO memberAddressDO = queryByUserId.get(0);
				MemberAddressDO memberAddress = memberAddressDao.get(memberAddressDO.getId());
				Map<String, Object> shopExpressMoneyMap = new HashMap<>();
				shopExpressMoneyMap.put("name", memberAddress.getProvince());
				shopExpressMoneyMap.put("delFlag", "0");
				List<ShopExpressMoneyDO> shopExpressMoneyList = shopExpressMoneyDao.likeList(shopExpressMoneyMap);
				BigDecimal postFee1 = null;
				if (shopExpressMoneyList.size() <= 0) {
					R.error("抱歉," + memberAddress.getProvince() + "地区不到!");
					// return
					// R.error("抱歉,"+memberAddress.getProvince()+"地区不到!");
				} else {
					
					//Double weight = isDefaultByGoods.getWeight();
					// 总重量
					weight = weight * 1;
					// 向上取整
					Double ceil = Math.ceil(weight);
					int intWeight = ceil.intValue();
					if (intWeight <= shopExpressMoneyList.get(0).getFirstWeight()) {
						postFee1 = shopExpressMoneyList.get(0).getFirstPrice();
					} else {
						// 邮费=首重价格+续重价格*续重kg //续重价格 //总重量-首重重量
						postFee1 = shopExpressMoneyList.get(0).getFirstPrice()
								.add(shopExpressMoneyList.get(0).getAddPrice().multiply(
										new BigDecimal(intWeight - shopExpressMoneyList.get(0).getFirstWeight())));
					}

				}
				productMap.put("post_fee",postFee1);
				
			}else{
				productMap.put("post_fee",postFee);
			}
		
			productMap.put("post_fee",postFee);
		}*/
		return productMap;
	}

	@Override
	public List<GoodsProductDO> getByGoodsId(String id) {
		HashMap<String, Object> objectObjectHashMap = MapUtil.newHashMap();
		objectObjectHashMap.put("goodsId",id);
		return goodsProductDao.getGoodsByGoodsId(objectObjectHashMap);

	}

}
